package b1.scenes.fight
{
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	import Box2D.Dynamics.*;
	
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.scenes.*;
	
	import com.greensock.*;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.text.*;
	import flash.utils.Timer;
	
	public class CrowEvent extends FightObject
	{
		//随机事件的图片和骨头，还有发生时候的位置
		private var randomEventTimer:Timer;
		//扔的骨头
		private var preparedBone:Bone;

		private var crowSprite:BasicMc;
		public var happenFlag:Boolean;
		private var speed:int;
		
		private const boneIndexArray:Array = [0,2,4,5,6,7,23];
		
		public function CrowEvent()
		{
			happenFlag = false;
			super(true);
		}
		
		public function startFly():void
		{
			happenFlag = true;
			//加载乌鸦
			var CrowImg:Class = ResourceHelper.getInstance().getClassByName("Crow");
			crowSprite = new BasicMc(CrowImg, 8);
			crowSprite.height = 50;
			addChild(crowSprite);
			speed = (Math.random() - 0.5) * 20;
			var minSpeed:Number = 2;
			//加载骨头
			var boneIndex:int = boneIndexArray[Math.floor(Math.random() * boneIndexArray.length)];
			preparedBone = new Bone(boneIndex);
			addChild(preparedBone);
			if (speed < 0)
			{
				speed -= minSpeed;
				this.x = 700;
				this.y = 150;
				crowSprite.scaleX = -crowSprite.scaleY;
				preparedBone.x = 0;
				preparedBone.y = 10;
			}
			else
			{
				speed += minSpeed;
				this.x = 10;
				this.y = 150;
				crowSprite.scaleX = crowSprite.scaleY;
				preparedBone.x = -preparedBone.width;
				preparedBone.y = 10;
			}
			randomEventTimer = new Timer(50);
			randomEventTimer.start();
			randomEventTimer.addEventListener(TimerEvent.TIMER, randomCrowOnTick);
			TweenLite.delayedCall(Math.random() * 10 / speed, startSpeak);
			TweenLite.delayedCall(Math.random() * 20 / speed, launchPrepareBone);
		}

		//发射准备的骨头
		public function launchPrepareBone():void
		{
			if (preparedBone == null)
				return;
			var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
			//由若干个骨头集结而成
			var mass:Number = 0;
			var boneConfig:ObjectConfig = Config.objectConfigs[preparedBone.index];
			var existTime:Number = 10;
			var localPoint:Point = new Point(preparedBone.x, preparedBone.y);
			var globalPoint:Point = localToGlobal(localPoint);
			var bonePosX:Number = globalPoint.x;
			var bonePosY:Number = globalPoint.y;
			var bone:Bone; 
			if (boneConfig.splitBoneNum > 0)
			{
				var boneCluster:BoneCluster = new BoneCluster(bonePosX, bonePosY, boneConfig.splitBoneIndex, boneConfig.splitBoneNum);
				for (var i:int = 0; i < boneCluster.bones.length; i++)
				{
					bone = boneCluster.bones[i];
					mass += bone.objectB2Body.GetMass();
					fight.objectLayer.addChild(bone);
					TweenLite.delayedCall(existTime, bone.destroy); 
				} 
				TweenLite.delayedCall(0.2, boneCluster.split);
			}
			else
			{
				bone = new Bone(preparedBone.index);
				var boneb2Body:b2Body = bone.createB2Body(fight.world, bonePosX, bonePosY, bone.width, bone.height, boneConfig.density, boneConfig.restitution);
				boneb2Body.SetAngle(this.rotation * Math.PI / 180);
				TweenLite.delayedCall(existTime, bone.destroy); 
				mass += bone.objectB2Body.GetMass();
				fight.objectLayer.addChild(bone);
			}
			var torque:Number = b2Math.RandomRange(0, 200) * mass;
			var forceX:Number = this.speed * 20 * mass;
			bone.applyForce(forceX, 0, torque)
			if (preparedBone != null && preparedBone.parent != null)
			{
				preparedBone.parent.removeChild(preparedBone);
				preparedBone = null;
			}
		}
		
		private function randomCrowOnTick(event:Event):void
		{
			this.x += speed;
			if ((this.x > 800 && speed > 0) || (this.x < 0 && speed < 0))
			{
				removeCrowEvent();
			}
		}
		
		public function removeCrowEvent():void
		{
			if (crowSprite != null && crowSprite.parent != null)
				crowSprite.parent.removeChild(crowSprite);
			if (null != randomEventTimer)
			{
				randomEventTimer.removeEventListener(TimerEvent.TIMER, randomCrowOnTick);
				randomEventTimer.stop();
			}
		}
		
		private function startSpeak():void
		{
			var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
			var ClueMsgBgrImage:Class = ResourceHelper.getInstance().getClassByName("DialogMsgBgrImage");
			var speakDialog:Sprite = new ClueMsgBgrImage();
			if (speed > 0)
			{
				speakDialog.x = 50;
				speakDialog.y = -speakDialog.height - 5;
			}
			else
			{
				speakDialog.x = -100;
				speakDialog.y = -speakDialog.height - 5;
			}
			addChild(speakDialog);
			var speakDialogText:TextField = new TextField();
			speakDialogText.selectable = false;
        	speakDialogText.autoSize = TextFieldAutoSize.LEFT;
        	speakDialogText.wordWrap = true;
        	speakDialogText.width = speakDialog.width * 0.8;
        	speakDialogText.defaultTextFormat = new TextFormat("微软雅黑", 14, 0x000000);
        	speakDialogText.text = Config.languageConfig.getLanguage(6);
			speakDialogText.x = 20;
			speakDialogText.y = 20;
			speakDialog.addChild(speakDialogText);
			TweenLite.delayedCall(3, closeSpeak, [speakDialog]);
		}
		
		private function closeSpeak(speakDialog:Sprite):void
		{
			if (speakDialog != null && speakDialog.parent != null)
			{
				speakDialog.parent.removeChild(speakDialog);
			}
		}
	}
}